package com.blackline.auto.Report;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Actions;

import com.blackline.auto.common.CommonMethods;
import com.blackline.declareUI.UI;
//import com.newb.AutoDriver.AutoDriver;
import com.blackline.parameters.*;

/**
 * @author Joie
 * @version Aug, 2013
 * <p>The folderoperation test for reporting.
 */
public class Reports_folderoperation {
	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger=Logger.getLogger(Reports_folderoperation.class);
	UI UI=new UI();
	Parameters parameters;
	private String expectErrorMsg;
	private String actualErrorMsg;
	
	@Before
	public void setUp() throws Exception {
		FirefoxProfile profile=new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters=new Parameters("parameters.xml");
		driver=new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		UI.declareAllUI(driver);
	}


	@Test
	public void Reportsfolderoperation() throws Exception {
		int sleepTime = 5000;
		Date now=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
		Calendar cal=Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.DAY_OF_MONTH, 1);
		String startDate=sdf.format(cal.getTime());
		logger.info(startDate);

		SimpleDateFormat sdf2=new SimpleDateFormat("yyyy");
		String years=sdf2.format(cal.getTime());
		String endDate="12/31/"+years;
		logger.info(endDate);

		
		logger.info("You've selected site: https://t2.blacklineondemand.com/Test2");
		UI.LogonPage.Login(driver, "https://t2.blacklineondemand.com/Test2", parameters.getLogonID("admin"), parameters.getPassword("admin"));
		Thread.sleep(3000);
		  UI.NavigationMenu.REPORTS.click();
		  UI.Reports.Enhanced_Reporting_link.click();
		  Thread.sleep(20000);
		  //FolderOperation under MyFavorite
		  UI.Reporting.My_Favorite_Reports_nodeInLeftTree.click();
		  Thread.sleep(3000);
		  
		  logger.info("---------Cancel add valid folder name-----------");
		  //Cancel add valid folder
		  UI.Reporting.Add_Folder_Link.click();
		  Thread.sleep(3000);
		  UI.Alert_inReporting.FolderName_Txt.sendKeys("TestFolder");
		  UI.Alert_inReporting.Cancel_button.click();
		  Thread.sleep(3000);
		  logger.info("Cancel add valid folder Pass");
		  Thread.sleep(3000);
		  
		  logger.info("---------Add valid folder name-----------");
		  //Add valid folder
		  UI.Reporting.Add_Folder_Link.click();
		  Thread.sleep(3000);
		  UI.Alert_inReporting.FolderName_Txt.sendKeys("TestFolder");
		  UI.Alert_inReporting.OK_button.click();
		  Thread.sleep(3000);
		  logger.info("Add Folder Pass");
		  Thread.sleep(3000);
		  
		  //Check the folder name unique 
		  logger.info("---------Check the folder name unique-----------");
		  UI.Reporting.Add_Folder_Link.click();
		  Thread.sleep(3000);
		  UI.Alert_inReporting.FolderName_Txt.sendKeys("TestFolder");
		  UI.Alert_inReporting.OK_button.click();
		  Thread.sleep(3000);
		  expectErrorMsg = "of duplicate folder exists";
		  actualErrorMsg=UI.Alert_inReporting.Message_Text.getText();
		  if(actualErrorMsg.endsWith(expectErrorMsg))
		  {
			  logger.info("Check the Folder unique Pass.");
		  }
		  else
		  {
			  logger.info("Check the Folder unique Failed.");
		  }
		  UI.Alert_inReporting.OK_button.click();
		  Thread.sleep(3000);
		  
		  logger.info("---------Rename folder name with valid characters-----------");
		  //Rename valid folder
		  UI.Reporting.TestFolder_Link.click();
		  Thread.sleep(3000);
		  UI.Reporting.Rename_Folder_Link.click();
		  Thread.sleep(3000);
		  UI.Alert_inReporting.FolderName_Txt.clear();
		  Thread.sleep(3000);
		  UI.Alert_inReporting.FolderName_Txt.sendKeys("RenameFolder");
		  UI.Alert_inReporting.OK_button.click();
		  Thread.sleep(3000);
		  logger.info("Rename Folder Pass");
		  
		  //Delete valid folder
		  UI.Reporting.Delete_Folder_Link.click();
		  Thread.sleep(3000);
		  UI.Alert_inReporting.OK_button.click();
		  Thread.sleep(3000);
		  logger.info("Delete Folder Pass");
		  
		  logger.info("---------Null folder Name Add Operation-----------");
		  //Add null folder
		  UI.Reporting.Add_Folder_Link.click();
		  Thread.sleep(3000);
		  UI.Alert_inReporting.FolderName_Txt.sendKeys("  ");
		  UI.Alert_inReporting.OK_button.click();
		  Thread.sleep(3000);
		  expectErrorMsg = "Folder Name: Must have value.";
		  actualErrorMsg=UI.Alert_inReporting.Message_Text.getText();
		  if(actualErrorMsg.endsWith(expectErrorMsg))
		  {
			  logger.info("Add Null Folder Pass.");
		  }
		  else
		  {
			  logger.info("Add Null Folder Failed.");
		  }
		  UI.Alert_inReporting.Cancel_button.click();
		  Thread.sleep(3000);
		  
		  
		  
		  logger.info("---------Invalid characters folder name add operation-----------");
		  //Add invalid characters folder
		  UI.Reporting.Add_Folder_Link.click();
		  Thread.sleep(3000);
		  UI.Alert_inReporting.FolderName_Txt.sendKeys("<script var='test'/>");
		  UI.Alert_inReporting.OK_button.click();
		  Thread.sleep(3000);
		  expectErrorMsg = "Folder Name: Text contains certain characters not allowed in input fields.";
		  actualErrorMsg=UI.Alert_inReporting.Message_Text.getText();
		  if(actualErrorMsg.endsWith(expectErrorMsg))
		  {
			  logger.info("Add Invalid char Folder Pass.");
		  }
		  else
		  {
			  logger.info("Add Invalid char Folder Failed.");
		  }
		  UI.Alert_inReporting.Cancel_button.click();
		  Thread.sleep(3000);
		  
}
		  

	@After
	public void tearDown() throws Exception {
		CommonMethods.takeScreenShot(this.getClass(), driver);
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
}

